package com.fjnldx.ks.service;

import com.alibaba.fastjson.JSONObject;
import com.fjnldx.ks.mapper.MesThirdMapper;
import com.fjnldx.ks.pojo.domain.*;
import com.fjnldx.ks.pojo.mesDoMain.*;
import com.fjnldx.ks.pojo.request.*;
import com.fjnldx.ks.pojo.response.*;
import com.fjnldx.ks.utils.Common;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class MesThirdService {
    @Resource
    MesThirdMapper mesThirdMapper;
    @Resource
    Common common;
    @Value("${ks.org}")
    private String org;
    @Value("${ks.cj}")
    private String cj;

    /**
     * 获取实时并条数据
     */
    public SchedulingShiftInModel getShiftInModel(String gx) {
        SchedulingShiftInModel inModel = new SchedulingShiftInModel(org, cj, gx, LocalDate.now());
        List<SchedulingShift> schedulingShifts = common.querySchedulingShift(inModel);
        if (schedulingShifts.size() > 0) {
            SchedulingShift shift = schedulingShifts.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            inModel.setBc(shift.getBc());
            return inModel;
        } else {
            return null;
        }
    }

    public Result getNewestBingTiao(Dev devSn) {
        SchedulingShiftInModel inModel = getShiftInModel("并条");
        if (inModel == null) {
            return Result.error();
        }
        inModel.setDevSn(devSn.getDevSn());
        Map<String, Object> result = mesThirdMapper.getNewestBingTiao(inModel);
        return Result.success(result);
    }

    /**
     * 获取实时细纱马佐里数据
     */
    public Result getNewestXiShaMzl(Dev devSn) {
        SchedulingShiftInModel inModel = getShiftInModel("细纱");
        if (inModel == null) {
            return Result.error();
        }
        inModel.setDevSn(devSn.getDevSn());
        Map<String, Object> res = mesThirdMapper.getNewestXiShaMzl(inModel);
        return Result.success(res);
    }

    /**
     * 获取实时络筒数据
     */
    public Result getNewestLuoTong(Dev devSn) {
        SchedulingShiftInModel inModel = getShiftInModel("络筒");
        if (inModel == null) {
            return Result.error();
        }
        inModel.setDevSn(devSn.getDevSn());
        SperoTable early = mesThirdMapper.getEarlyLt(inModel);
        SperoTable last = mesThirdMapper.getLastLt(inModel);
        List<SperoTable> lastLt = structureLt(last, early);
        inModel.setArrayLt(lastLt);
        List<Map<String,Object>> result = mesThirdMapper.querySpindleNumberMapping(inModel);
        for (Map<String, Object> map : result) {
            map.put("ds", Arrays.asList(String.valueOf(map.get("ds")).split(",")));
        }
        return Result.success(result);
    }

    public List<SperoTable> structureLt(SperoTable lastLt, SperoTable earlyLt) {
        JSONObject cx = JSONObject.parseObject(lastLt.getCx());
        JSONObject mj = JSONObject.parseObject(lastLt.getMj());
        JSONObject dc = JSONObject.parseObject(lastLt.getDc());
        JSONObject nj = JSONObject.parseObject(lastLt.getNj());
        JSONObject xh = JSONObject.parseObject(lastLt.getXh());
        JSONObject qc = JSONObject.parseObject(lastLt.getQc());
        JSONObject yj = JSONObject.parseObject(lastLt.getYj());
        JSONObject hg = JSONObject.parseObject(lastLt.getHg());
        JSONObject cz = JSONObject.parseObject(lastLt.getCz());
        JSONObject xq = JSONObject.parseObject(lastLt.getXq());
        JSONObject yx = JSONObject.parseObject(lastLt.getYx());
        JSONObject mt = JSONObject.parseObject(lastLt.getMt());
        JSONObject cc = JSONObject.parseObject(lastLt.getCc());
        JSONObject bc = JSONObject.parseObject(lastLt.getBc());
        JSONObject bcEarly = JSONObject.parseObject(earlyLt.getBc());
        JSONObject xl = JSONObject.parseObject(lastLt.getXl());
        List<SperoTable> result = new ArrayList<>();
        for (int i = 1; i <= 64; i++) {
            SperoTable item = new SperoTable();
            item.setDs(String.valueOf(i));
            item.setCx(String.valueOf(cx.get("cx" + i)));
            item.setMj(String.valueOf(mj.get("mj" + i)));
            item.setDc(String.valueOf(dc.get("dc" + i)));
            item.setNj(String.valueOf(nj.get("nj" + i)));
            item.setXh(String.valueOf(xh.get("xh" + i)));
            item.setQc(String.valueOf(qc.get("qc" + i)));
            item.setYj(String.valueOf(yj.get("yj" + i)));
            item.setHg(String.valueOf(hg.get("hg" + i)));
            item.setCz(String.valueOf(cz.get("cz" + i)));
            item.setXq(String.valueOf(xq.get("xq" + i)));
            item.setYx(String.valueOf(yx.get("yx" + i)));
            item.setMt(String.valueOf(mt.get("mt" + i)));
            item.setCc(String.valueOf(cc.get("cc" + i)));
            item.setBc(String.valueOf(Double.parseDouble(String.valueOf(bc.get("bc" + i))) - Double.parseDouble(String.valueOf(bcEarly.get("bc" + i)))));
            item.setDqcd(String.valueOf(bc.get("bc" + i)));
            item.setXl(String.valueOf(xl.get("xl" + i)));
            result.add(item);
        }
        return result;
    }

    /**
     * 获取络筒数据汇总
     */
    //public Result getHuiZongLuoTong(Dev devSn) throws Exception {
    //    SchedulingShiftInModel inModel = getShiftInModel("络筒");
    //    if (inModel == null) {
    //        return Result.error();
    //    }
    //    inModel.setDevSn(devSn.getDevSn());
    //    SperoTable nLt = mesThirdMapper.getNewestLuoTongTable(inModel);
    //    LuoTongHuiZong luoTongHuiZong = new LuoTongHuiZong();
    //    if (nLt != null) {
    //        luoTongHuiZong = common.huiZongLuoTong(nLt);
    //        luoTongHuiZong.setDqbc(nLt.getDqbc());
    //        luoTongHuiZong.setPz(nLt.getPz());
    //    }
    //    return Result.success(luoTongHuiZong);
    //
    //}

    // =========================================================

    /**
     * 获取实时精开棉机数据
     */
    public Result getNewestJingKaiMianJi(Dev devSn) {
        String devSn1 = devSn.getDevSn();
        Map<String, Object> newestJingkaimianjiTable = mesThirdMapper.getNewestJingkaimianjiTable(devSn1);
        return Result.success(newestJingkaimianjiTable);
    }

    /**
     * 获取实时多仓数据
     */
    public Result getNewestDuoCan(Dev devSn) {
        String devSn1 = devSn.getDevSn();
        Map<String, Object> newestDuoCanTable = mesThirdMapper.getNewestDuoCanTable(devSn1);
        return Result.success(newestDuoCanTable);
    }

    /**
     * 查询动力柜
     */
    public Result getDlg(List<DlgReq> list) {
        List<Dlg> dlg = new LinkedList<>();
        for (DlgReq dlgReq : list) {
            List<Dlg> dlgList = mesThirdMapper.getDlg(dlgReq);
            dlg.addAll(dlgList);
        }
        DlgSum dlgSum = new DlgSum();
        double zhdl = 0, dbhd = 0, dthd = 0, dyhd = 0, dzhd = 0, dl = 0;
        for (Dlg dlg1 : dlg) {
            dlg1.setTime(new Date());
            dlg1.setDzhd(dlg1.getDzhd() + dlg1.getDthd());//当周 = dian_table + dian_day_summary
            dlg1.setDyhd(dlg1.getDyhd() + dlg1.getDthd());//当月 = dian_table + dian_day_summary
            zhdl += dlg1.getZhd();
            dbhd += dlg1.getDbhd();
            dthd += dlg1.getDthd();
            dyhd += dlg1.getDyhd();
            dzhd += dlg1.getDzhd();
            dl += dlg1.getAvgI();
        }
        DecimalFormat dc = new DecimalFormat("0.0");
        dlgSum.setZhdl(dc.format(zhdl));
        dlgSum.setDbhdSum(dc.format(dbhd));
        dlgSum.setDthdSum(dc.format(dthd));
        dlgSum.setDyhdSum(dc.format(dyhd));
        dlgSum.setDzhdSum(dc.format(dzhd));
        dlgSum.setDlSum(dc.format(dl));

        DlgReturn dlgReturn = new DlgReturn();
        dlgReturn.setDlgList(dlg);
        dlgReturn.setDlgSum(dlgSum);
        Result result = new Result();
        if (dlg.size() > 0) {
            result = Result.success(dlgReturn);
        } else {
            result.error();
        }
        return result;
    }

    /**
     * 获取抓棉机实时数据
     */
    public Result getNewestZmj(Dev devSn) {
        String devSn1 = devSn.getDevSn();
        Map<String, Object> newestZmjTable = mesThirdMapper.getNewestZmjTable(devSn1);
        return Result.success(newestZmjTable);
    }

    /**
     * 获取实时梳棉机1207数据
     */
    public Result getNewestShuMianJi(Dev devSn) {
        String devSn1 = devSn.getDevSn();
        Map<String, Object> list = mesThirdMapper.getNewestShuMianJi(devSn1);
        return Result.success(list);
    }

    /**
     * 获取实时梳棉机1207A数据
     */
    public Result getNewestShumianjiaTable(Dev devSn) {
        String devSn1 = devSn.getDevSn();
        Map<String, Object> list = mesThirdMapper.getNewestShumianjiaTable(devSn1);
        return Result.success(list);
    }

    /**
     * 获取细纱各品种产量
     */
    public Result queryXsYieldVariety() {
        SchedulingShiftInModel inModel = new SchedulingShiftInModel(org, cj, "细纱", LocalDate.now());
        List<SchedulingShift> schedulingShifts = common.querySchedulingShift(inModel);
        if (schedulingShifts.size() > 0) {
            SchedulingShift shift = schedulingShifts.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            List<YieldOfEachVariety> list = mesThirdMapper.queryXsYieldVariety(inModel);
            YieldOfEachVarietyRes<YieldOfEachVariety> result = new YieldOfEachVarietyRes<>();
            result.setGroup(shift.getBc());
            result.setData(list);
            return Result.success(result);
        } else {
            return Result.error("请前往轮班管理安排【细纱】当日班次计划");
        }
    }

    /**
     * 车间开台比例-圆环
     */
    public Result workshopOpeningRatioRing() {
        List<WorkshopOpeningRatioRing> list = mesThirdMapper.workshopOpeningRatioRing();
        return Result.success(list);
    }

    /**
     * 车间开台比例-条形图
     */
    public Result workshopOpeningRatiBarChart() throws Exception {
        try {
            List<WorkshopOpening> res = mesThirdMapper.workshopAllDevSnOfGx();
            List<SjYx> sjYxes = mesThirdMapper.workshopOpeningRatioBarChartSjYx();
            List<JhKt> list = mesThirdMapper.workshopOpeningRatioBarChart();
            for (WorkshopOpening resItem : res) {
                for (JhKt item : list) {
                    if (item.getGongxu().equals(resItem.getGongxu())) {
                        resItem.setJhkts(item.getJhkt());
                    }
                }
                for (SjYx sjYx : sjYxes) {
                    if (sjYx.getGongxu().equals(resItem.getGongxu())) {
                        resItem.setSjyxct(sjYx.getSjyx());
                    }
                }
                if ("清花".equals(resItem.getGongxu())) {
                    resItem.setJhkts("10");
                    resItem.setSjyxct("10");
                }
            }
            return Result.success(res);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }

    }

    /**
     * 自络各品种产量
     */
    public Result querySpoolingYieldVariety() {
        SchedulingShiftInModel inModel = new SchedulingShiftInModel(org, cj, "络筒", LocalDate.now());
        List<SchedulingShift> schedulingShifts = common.querySchedulingShift(inModel);
        if (schedulingShifts.size() > 0) {
            SchedulingShift shift = schedulingShifts.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            List<YieldOfEachVariety> list = mesThirdMapper.querySpoolingYieldVariety(inModel);
            YieldOfEachVarietyRes<YieldOfEachVariety> result = new YieldOfEachVarietyRes<>();
            result.setGroup(shift.getBc());
            result.setData(list);
            return Result.success(result);
        } else {
            return Result.error("请前往轮班管理安排【络筒】当日班次计划");
        }

    }

    /**
     * 当前各工序产量-echart
     */
    public Result EachProcedureYield() {
        List<EachProcedureYield> list = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        SchedulingShiftInModel inModel = new SchedulingShiftInModel(org, cj, "", LocalDate.now());
        // 络筒
        inModel.setGongxu("络筒");
        List<SchedulingShift> shiftLt = common.querySchedulingShift(inModel);
        if (shiftLt.size() > 0) {
            SchedulingShift shift = shiftLt.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            EachProcedureYield spooling = mesThirdMapper.eachProcedureYieldSpooling(inModel);
            list.add(spooling);
        }
        // 细纱
        inModel.setGongxu("细纱");
        List<SchedulingShift> shiftXs = common.querySchedulingShift(inModel);
        if (shiftXs.size() > 0) {
            SchedulingShift shift = shiftXs.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            EachProcedureYield xs = mesThirdMapper.eachProcedureYieldXiSha(inModel);
            list.add(xs);
        }
        // 粗纱
        inModel.setGongxu("粗纱");
        List<SchedulingShift> shiftCs = common.querySchedulingShift(inModel);
        if (shiftCs.size() > 0) {
            SchedulingShift shift = shiftCs.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            EachProcedureYield cs = mesThirdMapper.eachProcedureYieldCuSha(inModel);
            list.add(cs);
        }
        // 梳棉
        inModel.setGongxu("梳棉");
        List<SchedulingShift> shiftSm = common.querySchedulingShift(inModel);
        if (shiftSm.size() > 0) {
            SchedulingShift shift = shiftSm.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            EachProcedureYield sm = mesThirdMapper.eachProcedureYieldShuMian(inModel);
            list.add(sm);
        }
        // 并条
        inModel.setGongxu("并条");
        List<SchedulingShift> shiftBt = common.querySchedulingShift(inModel);
        if (shiftBt.size() > 0) {
            SchedulingShift shift = shiftBt.get(0);
            LocalDateTime startTime = shift.getStartTime();
            LocalDateTime endTime = shift.getEndTime();
            inModel.setStartTime(formatter.format(startTime));
            inModel.setEndTime(formatter.format(endTime));
            EachProcedureYield bt = mesThirdMapper.eachProcedureYieldBingTiao(inModel);
            list.add(bt);
        }
        return Result.success(list);
    }

    /**
     * 细纱1.5级用工数据
     */
    public Result queryUseWorkersDetails() throws Exception {
        try {
            List<UseWorkersDetails> list = mesThirdMapper.queryUseWorkersDetailsNight();
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }
    }

    /**
     * 细纱1.5级用工数据-汇总
     */
    public Result queryUseWorkersDetailsSummary() throws Exception {
        try {
            List<UseWorkersDetailsSum> list = mesThirdMapper.queryUseWorkersDetailsNightSummary();
            for (UseWorkersDetailsSum item : list) {
                List<DevSnAreaList> devSnList = mesThirdMapper.queryUseWorkersDetailsEquipmentSection("细纱", item.getDcg(), item.getPz());
                String[] devSnArea = new String[devSnList.size()];
                for (int i = 0; i < devSnList.size(); i++) {
                    devSnArea[i] = devSnList.get(i).getDevSn();
                }
                item.setDevSnArea(devSnArea);
            }
            return Result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }
    }

    /**
     * 细纱1.5级明细数据
     */
    public Result queryXiShaDefiniteAndDetailed() {
        SchedulingShiftInModel inModel = getShiftInModel("细纱");
        if (inModel == null) {
            return Result.error();
        }
        List<DefiniteAndDetailed> list = mesThirdMapper.queryXiShaDefiniteAndDetailed(inModel);
        return Result.success(list);
    }

    /**
     * 粗纱1.5级明细数据
     */
    public Result queryDefiniteAndDetailedCs() {
        List<DefiniteAndDetailed> list = mesThirdMapper.queryDefiniteAndDetailedCs();
        return Result.success(list);
    }

    /**
     * 梳棉1.5级明细数据
     */
    public Result queryDefiniteAndDetailedSm() {
        List<DefiniteAndDetailed> list = mesThirdMapper.queryDefiniteAndDetailedSm();
        return Result.success(list);
    }

    /**
     * 并条1.5级明细数据
     */
    public Result queryDefiniteAndDetailedBt() {
        List<DefiniteAndDetailed> list = mesThirdMapper.queryDefiniteAndDetailedBt();
        return Result.success(list);
    }

    /**
     * 络筒1.5级明细数据
     */
    public Result queryDefiniteAndDetailedLt() {
        List<DefiniteAndDetailed> list = mesThirdMapper.queryDefiniteAndDetailedLt();
        return Result.success(list);
    }

    /**
     * 获取各工序电量
     */
    public Result getImpEpDian() throws Exception {
        try {
            List<ImpEpSum> impEpSums = mesThirdMapper.queryImpEp();
            ArrayList<ImpEpSum> impEpSums2 = new ArrayList<>();
            double qianSum = 0;
            for (ImpEpSum impEpSum : impEpSums) {
                if (!"多仓".equals(impEpSum.getGongxu()) && !"抓包".equals(impEpSum.getGongxu()) && !"精开".equals(impEpSum.getGongxu()) && !"梳棉".equals(impEpSum.getGongxu()) && !"粗纱".equals(impEpSum.getGongxu())) {
                    impEpSums2.add(impEpSum);
                } else {
                    qianSum += Double.parseDouble(impEpSum.getImpEp());
                }
            }
            ImpEpSum frontSpinning = new ImpEpSum();
            frontSpinning.setGongxu("前纺");
            frontSpinning.setImpEp(String.valueOf(qianSum));
            impEpSums2.add(frontSpinning);
            return Result.success(impEpSums2);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception();
        }
    }

    /**
     * 获取温湿度数据
     */
    public Result queryWenShiDu(TimeReq timeReq) {
        List<WenshiduTable> wenshiduTables = mesThirdMapper.queryWenShiDu(timeReq);
        DianHaoReq dianHaoReq = new DianHaoReq(timeReq);
        dianHaoReq.setGongXu("空调");
        List<DianLiRes> dianLiResList = new ArrayList<>();
        dianLiResList = mesThirdMapper.queryImpEpByTime(dianHaoReq);
        WenShiDianRes wenShiDianRes = new WenShiDianRes();
        wenShiDianRes.setHaoDianList(dianLiResList);
        wenShiDianRes.setWenShiDuList(wenshiduTables);
        return Result.success(wenShiDianRes);
    }

    /**
     * 获取实时太行粗纱欧姆龙数据
     *
     * @return
     */
    public Result getNewestTxcsoml(Dev devSn) {
        Map<String, Object> list = mesThirdMapper.getNewestTxcsoml(devSn.getDevSn());
        return Result.success(list);
    }

    /**
     * 获取实时太行粗纱欧姆龙数据-新设备
     *
     * @return
     */
    public Result getNewestTxcsomlNew(Dev devSn) {
        Map<String, Object> list = mesThirdMapper.getNewestTxcsomlNew(devSn.getDevSn());
        return Result.success(list);
    }

    /**
     * 获取细纱分组数据
     */
    public Result getGroupXiSha() throws Exception {
        List<EquipmentFloatingWindow> groupXishaArr1 = mesThirdMapper.getGroupXishaArr1();
        return Result.success(groupXishaArr1);
    }

    /**
     * 获取细纱分组实时数据-1.5级车间
     */
    public Result getGroupNewestXiSha(XsReq xsReq) {
        SchedulingShiftInModel inModel = getShiftInModel("细纱");
        if (inModel == null) {
            return Result.error();
        }
        List<DetailByPz> groupNewestXiShaMzl = mesThirdMapper.getGroupNewestXiShaMzl(inModel);
        for (DetailByPz detailByPz : groupNewestXiShaMzl) {
            List<DevSnAreaList> list = mesThirdMapper.getXiShaDevsnAreaByPz(detailByPz.getPz());
            String[] devSnArea = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                devSnArea[i] = list.get(i).getDevSn();
            }
            detailByPz.setDevSnArea(devSnArea);
        }
        return Result.success(groupNewestXiShaMzl);
    }

    /**
     * 获取太行粗纱欧姆龙分组数据
     */
    public Result getGroupTxcsoml() throws Exception {
        List<EquipmentFloatingWindow> groupTxcsomlArr1 = mesThirdMapper.getGroupTxcsomlArr1();
        return Result.success(groupTxcsomlArr1);
    }

    /**
     * 获取粗纱分组实时数据-1.5级-车间
     */
    public Result getGroupNewestCusha(XsReq xsReq) {
        List<DetailByPz> list2 = mesThirdMapper.getGroupNewestCusha(xsReq);
        for (DetailByPz detailByPz : list2) {
            List<DevSnAreaList> list = mesThirdMapper.getCuShaDevsnAreaByPz(detailByPz.getPz());
            String[] devSnArea = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                devSnArea[i] = list.get(i).getDevSn();
            }
            detailByPz.setDevSnArea(devSnArea);
        }
        return Result.success(list2);
    }

    /**
     * 获取络筒分组实时数据-1.5级车间
     */
    public Result getGroupNewestLuoTong(XsReq xsReq) {
        List<DetailByPz> list = mesThirdMapper.getGroupNewestLuoTong(xsReq);
        for (DetailByPz detailByPz : list) {
            List<DevSnAreaList> list2 = mesThirdMapper.getLuoTongDevsnAreaByPz(detailByPz.getPz());
            String[] devSnArea = new String[list2.size()];
            for (int i = 0; i < list2.size(); i++) {
                devSnArea[i] = list2.get(i).getDevSn();
            }
            detailByPz.setDevSnArea(devSnArea);
        }
        return Result.success(list);
    }

    /**
     * 获取并条分组实时数据-1.5级车间
     */
    public Result getGroupNewestBingTiao(XsReq xsReq) {
        List<DetailByPz> list = mesThirdMapper.getGroupNewestBingTiao(xsReq);
        for (DetailByPz detailByPz : list) {
            List<DevSnAreaList> list2 = mesThirdMapper.getBingTiaoDevsnAreaByPz(detailByPz.getPz());
            String[] devSnArea = new String[list2.size()];
            for (int i = 0; i < list2.size(); i++) {
                devSnArea[i] = list2.get(i).getDevSn();
            }
            detailByPz.setDevSnArea(devSnArea);
        }
        return Result.success(list);
    }

    /**
     * 获取梳棉分组实时数据-1.5级车间
     */
    public Result getGroupNewestShuMian(XsReq xsReq) throws Exception {
        try {
            List<DetailByPz> list = mesThirdMapper.getGroupNewestShuMian(xsReq);
            for (DetailByPz detailByPz : list) {
                List<DevSnAreaList> list2 = mesThirdMapper.getShuMianDevsnAreaByPz(detailByPz.getPz());
                String[] devSnArea = new String[list2.size()];
                for (int i = 0; i < list2.size(); i++) {
                    devSnArea[i] = list2.get(i).getDevSn();
                }
                detailByPz.setDevSnArea(devSnArea);
                if (detailByPz.getDh() != null && detailByPz.getSjclsum() != null) {
                    double sjclsum = Math.abs(Double.parseDouble(detailByPz.getSjclsum()));
                    double dh = Double.parseDouble(detailByPz.getDh());
                    detailByPz.setDshd(String.format("%.2f", dh * 1000 / sjclsum));
                }
            }
            return Result.success(list);
        } catch (Exception e) {
            throw new Exception();
        }
    }

    /**
     * 获取清花分组实时数据-1.5级页面表格
     */
    public Result getGroupNewestQingHua(XsReq xsReq) {
        DetailByPz detail = new DetailByPz();
        detail.setPz("RAA-5");
        //detail.setDevSnArea(xsReq.getStart() + "-" + xsReq.getEnd());
        detail.setSjclsum("100");
        detail.setJhclsum("102");
        detail.setXiaolvavg("92");
        ArrayList<DetailByPz> detailList = new ArrayList<>();
        detailList.add(detail);
        return Result.success(detailList);
    }

    /**
     * 获取并条分组数据
     */
    public Result getGroupBingTiao() {
        List<EquipmentFloatingWindow> groupBingTiaoArr1 = mesThirdMapper.getGroupBingTiaoArr1();
        return Result.success(groupBingTiaoArr1);
    }

    /**
     * 获取络筒分组数据
     */
    public Result getGroupLuoTong() {
        List<EquipmentFloatingWindow> groupLuoTongArr = mesThirdMapper.getGroupLuoTongArr();
        return Result.success(groupLuoTongArr);
    }

    /**
     * 获取梳棉机1207分组数据
     */
    public TxcsomlResult getGroupShuMianJi() {
        List<EquipmentFloatingWindow> groupShuMianJiArr1 = mesThirdMapper.getGroupShuMianJiArr1();
        List<EquipmentFloatingWindow> groupShuMianJiAArr2 = mesThirdMapper.getGroupShuMianJiAArr2();
        //List<EquipmentFloatingWindow> equipmentFloatingWindows = common.complateData(1, 16, groupShuMianJiArr1);
        //List<EquipmentFloatingWindow> equipmentFloatingWindow = common.complateData(17, 32, groupShuMianJiAArr2);
        //common.sortData(equipmentFloatingWindows);
        //common.sortData(equipmentFloatingWindow);
        TxcsomlResult txcsomlResult = new TxcsomlResult();
        txcsomlResult.setCode(1);
        txcsomlResult.setStatus("success");
        txcsomlResult.setMsg("查询成功");
        txcsomlResult.setData1(groupShuMianJiArr1);
        txcsomlResult.setData2(groupShuMianJiAArr2);
        return txcsomlResult;
    }

    /**
     * 获取多仓分组数据
     */
    public Result getGroupDuoCan() {
        List<EquipmentFloatingWindow> groupDuoCanArr = mesThirdMapper.getGroupDuoCanArr();
        //List<EquipmentFloatingWindow> equipmentFloatingWindows = common.complateData(1, 4, groupDuoCanArr);
        //common.sortData(equipmentFloatingWindows);
        return Result.success(groupDuoCanArr);
    }

    /**
     * 获取电表分组数据
     */
    public Result getGroupDianBiao() {
        List<DianBiao> groupDianBiaoArr = mesThirdMapper.getGroupDianBiaoArr();
        return Result.success(groupDianBiaoArr);
    }

    /**
     * 获取精开棉机分组数据
     */
    public Result getGroupJingKaiMianJi() {
        List<EquipmentFloatingWindow> groupJingKaiMianJiArr = mesThirdMapper.getGroupJingKaiMianJiArr();
        //List<EquipmentFloatingWindow> equipmentFloatingWindows = common.complateData(1, 4, groupJingKaiMianJiArr);
        //common.sortData(equipmentFloatingWindows);
        return Result.success(groupJingKaiMianJiArr);
    }

    /**
     * 获取抓棉机分组数据
     */
    public Result getGroupZmj() {
        List<EquipmentFloatingWindow> groupZmjArr = mesThirdMapper.getGroupZmjArr();
        //List<EquipmentFloatingWindow> equipmentFloatingWindows = common.complateData(1, 2, groupZmjArr);
        //common.sortData(equipmentFloatingWindows);
        return Result.success(groupZmjArr);
    }

    /**
     * 车间用工
     */
    public Result getCjyg() {
        //List<CjygGx> cjygGx = mesThirdMapper.getCjygGx();
        ArrayList<CjygGx> cjygGx = new ArrayList<>();
        CjygGx qs = new CjygGx();
        qs.setGongxu("清梳");
        qs.setSjyg("12");
        qs.setWdyg("1.5");
        CjygGx bt = new CjygGx();
        bt.setGongxu("并条");
        bt.setSjyg("13");
        bt.setWdyg("1.625");
        CjygGx cs = new CjygGx();
        cs.setGongxu("粗纱");
        cs.setSjyg("22");
        cs.setWdyg("2.75");
        CjygGx xs = new CjygGx();
        xs.setGongxu("细纱");
        xs.setSjyg("116");
        xs.setWdyg("14.5");
        CjygGx lt = new CjygGx();
        lt.setGongxu("络筒");
        lt.setSjyg("65");
        lt.setWdyg("8.125");
        cjygGx.add(qs);
        cjygGx.add(bt);
        cjygGx.add(cs);
        cjygGx.add(xs);
        cjygGx.add(lt);
        //CjygSum cjygSum = mesThirdMapper.getCjygSum();
        CjygSum cjygSum = new CjygSum();
        cjygSum.setZyg("228");
        cjygSum.setWdyg("28.5");
        CjygReturn cjygReturn = new CjygReturn();
        cjygReturn.setCjygSum(cjygSum);
        cjygReturn.setCjygGxList(cjygGx);
        return Result.success(cjygReturn);
    }


    /**
     * 查询每个时间点上压力机的数值
     *
     * @param pointInTimeReq
     * @return
     */
    public Result getYlByPointInTime(YlByPointInTimeReq pointInTimeReq) {
        String devSn = pointInTimeReq.getDevSn();
        YaLiResult result = new YaLiResult();
        List<String> yaLis = new ArrayList<>();
        List<String> times = new ArrayList<>();
        // 获取时间点
        List<String> pointInTimes = common.getPointInTimes(pointInTimeReq.getBeginTime(), pointInTimeReq.getEndTime());
        // 循环查询时间点上压力机的压力
        for (String pointInTime : pointInTimes) {
            String yaLiValue = mesThirdMapper.getYlByPointInTime(pointInTime, devSn);
            // 如果查不出来压力值, 则查找前后5分钟内的压力值
            if (yaLiValue == null) {
                yaLiValue = mesThirdMapper.getYlByLatelyTime(pointInTime, devSn);
            }
            yaLiValue = yaLiValue == null ? "0" : yaLiValue;
            // 将时间点的格式改为 HH:mm
            pointInTime = new StringBuilder(pointInTime.substring(pointInTime.length() - 4, pointInTime.length())).insert(2, ":").toString();
            yaLis.add(yaLiValue);
            times.add(pointInTime);
        }
        result.setTimes(times);
        result.setYaLis(yaLis);
        return Result.success(result);
    }

    /**
     * 根据设备号获取电流
     *
     * @param devSn
     * @return
     */
    public Result getDlByDevSnReq(DlByDevSnReq devSn) {
        List<DianLiu> dianLiuList = mesThirdMapper.getDlByDevSnReq(devSn);
        for (DianLiu dianLiu : dianLiuList) {
            if (Double.parseDouble(dianLiu.getIavg()) <= 0.00) {
                dianLiu.setOpen(false);
            } else {
                dianLiu.setOpen(true);
            }
        }
        return Result.success(dianLiuList);
    }

    /**
     * 获取所有设备的最近的电流
     *
     * @return
     */
    public Result getLatelyDlByDevSnReq() {
        // 获取所有的空压机设备号
        List<DlByDevSnReq> allDevSn = mesThirdMapper.getAllDevSn();
        String devSn = "";
        List<DianLiu> dianLiuList = new ArrayList<>();
        // 查询所有空压机的最近一次的电流
        for (DlByDevSnReq devSnReq : allDevSn) {
            devSn = devSnReq.getDevSn();
            DianLiu dianLiu = mesThirdMapper.getLatelyDlByDevSnReq(devSn);
            if (Double.valueOf(dianLiu.getIavg()) <= 0.00) {
                dianLiu.setOpen(false);
            } else {
                dianLiu.setOpen(true);
            }
            dianLiuList.add(dianLiu);
        }
        return Result.success(dianLiuList);
    }


    /**
     * 查询1.5各工序用工数据
     */
    public Result queryXiShaYg(Gx gx) {
        // 确认薪资计算方法后工具工序查询各工序用工数据
        switch (gx.getGongxu()) {
            case "络筒":
                List<GongXuYg> ygLt = mesThirdMapper.getYgLt();
                return Result.success(ygLt);
            case "粗纱":
                List<GongXuYg> ygCs = mesThirdMapper.getYgCs();
                return Result.success(ygCs);
            case "梳棉":
                List<GongXuYg> ygSm = mesThirdMapper.getYgSm();
                return Result.success(ygSm);
            case "并条":
                List<GongXuYg> ygBt = mesThirdMapper.getYgBt();
                return Result.success(ygBt);
            default:
                return null;
        }
    }

    /**
     * 粗纱1.5用工汇总数据
     */
    public Result employmentSummaryCs() {
        List<UseWorkersDetailsSum> list = mesThirdMapper.employmentSummaryCs();
        for (UseWorkersDetailsSum item : list) {
            List<DevSnAreaList> devSnList = mesThirdMapper.queryUseWorkersDetailsEquipmentSection("粗纱", item.getDcg(), item.getPz());
            String[] devSnArea = new String[devSnList.size()];
            for (int i = 0; i < devSnList.size(); i++) {
                devSnArea[i] = devSnList.get(i).getDevSn();
            }
            item.setDevSnArea(devSnArea);
        }
        return Result.success(list);
    }

    /**
     * 梳棉1.5用工汇总数据
     */
    public Result employmentSummarySm() {
        List<UseWorkersDetailsSum> list = mesThirdMapper.employmentSummarySm();
        for (UseWorkersDetailsSum item : list) {
            List<DevSnAreaList> devSnList = mesThirdMapper.queryUseWorkersDetailsEquipmentSection("梳棉", item.getDcg(), item.getPz());
            String[] devSnArea = new String[devSnList.size()];
            for (int i = 0; i < devSnList.size(); i++) {
                devSnArea[i] = devSnList.get(i).getDevSn();
            }
            item.setDevSnArea(devSnArea);
        }
        return Result.success(list);
    }

    /**
     * 并条5用工汇总数据
     */
    public Result employmentSummaryBt() {
        List<UseWorkersDetailsSum> list = mesThirdMapper.employmentSummaryBt();
        for (UseWorkersDetailsSum item : list) {
            List<DevSnAreaList> devSnList = mesThirdMapper.queryUseWorkersDetailsEquipmentSection("并条", item.getDcg(), item.getPz());
            String[] devSnArea = new String[devSnList.size()];
            for (int i = 0; i < devSnList.size(); i++) {
                devSnArea[i] = devSnList.get(i).getDevSn();
            }
            item.setDevSnArea(devSnArea);
        }
        return Result.success(list);
    }

    /**
     * 络筒1.5用工汇总数据
     */
    public Result employmentSummaryLt() {
        List<UseWorkersDetailsSum> list = mesThirdMapper.employmentSummaryLt();
        for (UseWorkersDetailsSum item : list) {
            List<DevSnAreaList> devSnList = mesThirdMapper.queryUseWorkersDetailsEquipmentSection("络筒", item.getDcg(), item.getPz());
            String[] devSnArea = new String[devSnList.size()];
            for (int i = 0; i < devSnList.size(); i++) {
                devSnArea[i] = devSnList.get(i).getDevSn();
            }
            item.setDevSnArea(devSnArea);
        }
        return Result.success(list);
    }

    /**
     * 历史轨迹数据实时查询
     *
     * @param dateSectionReq
     * @return
     */
    public Result getHistoryTrackData(DateSectionReq dateSectionReq) {
        LocalDate beginDate = dateSectionReq.getBeginDate();
        LocalDate endDate = dateSectionReq.getEndDate();
        LocalDateTime beginTime = beginDate.atTime(LocalTime.MIN);
        LocalDateTime endTime = endDate.atTime(LocalTime.MIN);
        List<XsHistoryResult> xsHistoryResults = new ArrayList<>();
        // 获取开始日期和结束日期中的每个日期
        List<String> days = common.getDaysBySection(beginTime, endTime);
        DateSectionQueryReq dateSectionQueryReq = new DateSectionQueryReq();
        dateSectionQueryReq.setDateSectionReq(dateSectionReq);
        for (String day : days) {
            String tableName = "xs_" + day;
            // 查询这个日期是否有数据
            Integer count = mesThirdMapper.isHaveDataByXs(tableName);
            if (count > 0) {
                // 获取日期的时间点
                LocalDateTime beginTimeByPointTime = LocalDateTime.of(beginTime.toLocalDate(), LocalTime.MIN);
                LocalDateTime endTimeByPointTime = LocalDateTime.of(endTime.toLocalDate(), LocalTime.MAX);
                List<String> pointInTimes = common.getPointInTimes(beginTimeByPointTime, endTimeByPointTime);
                // 根据时间点查询数据
                for (int i = 0; i < pointInTimes.size(); i++) {
                    String pointInTime = pointInTimes.get(i);
                    dateSectionQueryReq.setPointInTime(pointInTime);
                    dateSectionQueryReq.setTableName(tableName);
                    XsHistory xsHistory = mesThirdMapper.getHistoryTrackData(dateSectionQueryReq);
                    LocalDateTime pointInTimeLocalDate = LocalDateTime.parse(pointInTime, DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
                    // 如果查询的时间超过了现在, 则不在取上个时刻的数据
                    if (pointInTimeLocalDate.compareTo(LocalDateTime.now()) < 1) {
                        if (xsHistory == null && i != 0) {
                            xsHistory = xsHistoryResults.get(i - 1).getXsHistoryResult();
                        }
                    }
                    xsHistory = xsHistory == null ? new XsHistory() : xsHistory;
                    XsHistoryResult xsHistoryResult = new XsHistoryResult();
                    xsHistoryResult.setXsHistoryResult(xsHistory);
                    pointInTime = common.dateTimeFormat(pointInTime, "yyyyMMddHHmm", "yyyy-MM-dd HH:mm");
                    xsHistoryResult.setTime(pointInTime);
                    xsHistoryResults.add(xsHistoryResult);
                }
            }
        }
        return Result.success(xsHistoryResults);
    }

    /**
     * 获取细纱1.5级折线图数据
     */
    public Result getLineChartData(String devSn) {
        List<LineChartForA> list = mesThirdMapper.getLineChartData2(devSn);
        return Result.success(list);
    }

    /**
     * 获取落纱工工资
     */
    public Result getDailySalaryOfLs(DailySalary data) {
        List<LsSalary> list = mesThirdMapper.getDailySalaryOfLs(data);
        List<LsSalary> listCaptain = mesThirdMapper.getDailySalaryOfLsCaptain(data);
        list.addAll(listCaptain);
        return Result.success(list);
    }

    /**
     * 细纱车台运行状态统计
     */
    public Result runningState() {
        List<RunningState> runningStates = mesThirdMapper.runningState();
        return Result.success(runningStates);
    }
}
